package com.healMyself.util;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class C3p0Conn {
    private static final ComboPooledDataSource dataSource = new ComboPooledDataSource();

    static ThreadLocal<Connection> local = new ThreadLocal();

    /**
     * 获取c3p0连接池连接对象
     * @return
     * @throws SQLException
     */
    public static Connection getConn() throws SQLException {
        if (local.get() == null)
            local.set(dataSource.getConnection());
        return local.get();
    }

    /**
     * 关闭资源
     * @param set
     * @param statement
     * @param conn
     */
    public static void close(ResultSet set, Statement statement, Connection conn){
        try {
            if (set != null)
                set.close();
            if (statement != null)
                statement.close();
            if (conn != null)
                conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
